<template>
  <div class="message-notification-settings">
    <!-- 通知方式设置 -->
    <div class="setting-section">
      <div class="section-title">通知方式</div>
      <div class="section-content">
        <div 
          v-for="method in notificationMethods" 
          :key="method.value"
          class="method-item"
          @click="toggleMethod(method.value)"
        >
          <div class="method-info">
            <i :class="method.icon + ' method-icon'"></i>
            <div class="method-text">
              <div class="method-name">{{ method.label }}</div>
              <div class="method-desc">{{ method.description }}</div>
            </div>
          </div>
          <el-switch
            v-model="settings.methods[method.value]"
            @click.native.stop
            @change="handleMethodChange"
          ></el-switch>
        </div>
      </div>
    </div>

    <!-- 通知时间段设置 -->
    <div class="setting-section">
      <div class="section-title">通知时间段</div>
      <div class="section-content">
        <div class="time-range-item">
          <div class="time-label">开始时间</div>
          <el-time-picker
            v-model="settings.timeRange.start"
            placeholder="选择开始时间"
            format="HH:mm"
            value-format="HH:mm"
            style="width: 180px;"
          ></el-time-picker>
        </div>
        <div class="time-range-item">
          <div class="time-label">结束时间</div>
          <el-time-picker
            v-model="settings.timeRange.end"
            placeholder="选择结束时间"
            format="HH:mm"
            value-format="HH:mm"
            style="width: 180px;"
          ></el-time-picker>
        </div>
        <div class="time-tip">
          <i class="el-icon-info"></i>
          <span>在设置的时间段内，系统将按照选择的方式推送通知</span>
        </div>
      </div>
    </div>

    <!-- 通知类型控制 -->
    <div class="setting-section">
      <div class="section-title">通知类型控制</div>
      <div class="section-content">
        <div 
          v-for="type in notificationTypes" 
          :key="type.value"
          class="type-item"
        >
          <div class="type-info">
            <i :class="type.icon + ' type-icon'"></i>
            <div class="type-text">
              <div class="type-name">{{ type.label }}</div>
              <div class="type-desc">{{ type.description }}</div>
            </div>
          </div>
          <el-switch
            v-model="settings.types[type.value]"
            :disabled="!hasActiveMethod"
          ></el-switch>
        </div>
      </div>
    </div>

    <!-- 免打扰模式 -->
    <div class="setting-section">
      <div class="section-title">免打扰模式</div>
      <div class="section-content">
        <div class="dnd-item">
          <div class="dnd-info">
            <i class="el-icon-moon-night dnd-icon"></i>
            <div class="dnd-text">
              <div class="dnd-name">开启免打扰</div>
              <div class="dnd-desc">开启后将不接收任何非紧急通知</div>
            </div>
          </div>
          <el-switch
            v-model="settings.doNotDisturb.enabled"
            @change="handleDndChange"
          ></el-switch>
        </div>
        
        <div v-if="settings.doNotDisturb.enabled" class="dnd-time-range">
          <div class="dnd-time-item">
            <div class="time-label">免打扰开始</div>
            <el-time-picker
              v-model="settings.doNotDisturb.startTime"
              placeholder="开始时间"
              format="HH:mm"
              value-format="HH:mm"
              style="width: 180px;"
            ></el-time-picker>
          </div>
          <div class="dnd-time-item">
            <div class="time-label">免打扰结束</div>
            <el-time-picker
              v-model="settings.doNotDisturb.endTime"
              placeholder="结束时间"
              format="HH:mm"
              value-format="HH:mm"
              style="width: 180px;"
            ></el-time-picker>
          </div>
        </div>

        <div class="emergency-override">
          <el-checkbox v-model="settings.doNotDisturb.emergencyOverride">
            紧急事件通知始终接收（不受免打扰影响）
          </el-checkbox>
        </div>
      </div>
    </div>

    <!-- 通知音效设置 -->
    <div class="setting-section">
      <div class="section-title">通知音效</div>
      <div class="section-content">
        <div class="sound-item">
          <div class="sound-info">
            <i class="el-icon-bell sound-icon"></i>
            <div class="sound-text">
              <div class="sound-name">通知提示音</div>
              <div class="sound-desc">选择通知到达时的提示音</div>
            </div>
          </div>
          <el-select 
            v-model="settings.sound.tone" 
            placeholder="选择提示音"
            style="width: 120px;"
          >
            <el-option label="默认" value="default"></el-option>
            <el-option label="清脆" value="crisp"></el-option>
            <el-option label="柔和" value="soft"></el-option>
            <el-option label="紧急" value="urgent"></el-option>
            <el-option label="静音" value="silent"></el-option>
          </el-select>
        </div>
        
        <div class="sound-item">
          <div class="sound-info">
            <i class="el-icon-mobile-phone sound-icon"></i>
            <div class="sound-text">
              <div class="sound-name">振动提醒</div>
              <div class="sound-desc">通知时同时震动提醒</div>
            </div>
          </div>
          <el-switch v-model="settings.sound.vibrate"></el-switch>
        </div>
      </div>
    </div>

    <!-- 通知频率限制 -->
    <div class="setting-section">
      <div class="section-title">通知频率限制</div>
      <div class="section-content">
        <div class="frequency-item">
          <div class="frequency-label">同类通知最小间隔时间</div>
          <el-slider
            v-model="settings.frequency.interval"
            :min="1"
            :max="60"
            :step="5"
            show-stops
            :marks="frequencyMarks"
          ></el-slider>
          <div class="frequency-tip">
            当前设置：同类通知至少间隔 <span class="highlight">{{ settings.frequency.interval }}</span> 分钟
          </div>
        </div>
      </div>
    </div>

    <!-- 消息推送偏好 -->
    <div class="setting-section">
      <div class="section-title">消息推送偏好</div>
      <div class="section-content">
        <div class="preference-item">
          <div class="preference-info">
            <i class="el-icon-finished preference-icon"></i>
            <div class="preference-text">
              <div class="preference-name">自动已读</div>
              <div class="preference-desc">点击通知后自动标记为已读</div>
            </div>
          </div>
          <el-switch v-model="settings.preferences.autoRead"></el-switch>
        </div>

        <div class="preference-item">
          <div class="preference-info">
            <i class="el-icon-document-copy preference-icon"></i>
            <div class="preference-text">
              <div class="preference-name">消息预览</div>
              <div class="preference-desc">在通知栏显示消息内容预览</div>
            </div>
          </div>
          <el-switch v-model="settings.preferences.preview"></el-switch>
        </div>

        <div class="preference-item">
          <div class="preference-info">
            <i class="el-icon-warning-outline preference-icon"></i>
            <div class="preference-text">
              <div class="preference-name">角标提示</div>
              <div class="preference-desc">应用图标显示未读消息数量</div>
            </div>
          </div>
          <el-switch v-model="settings.preferences.badge"></el-switch>
        </div>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons">
      <el-button 
        size="large"
        @click="handleReset"
      >
        恢复默认
      </el-button>
      <el-button 
        type="primary" 
        size="large"
        @click="handleSave"
      >
        保存设置
      </el-button>
    </div>

    <!-- 恢复默认确认对话框 -->
    <el-dialog
      title="恢复默认设置"
      :visible.sync="showResetDialog"
      width="80%"
      center
      :close-on-click-modal="false"
    >
      <div class="reset-confirm-content">
        <i class="el-icon-warning reset-icon"></i>
        <p class="reset-message">确定要恢复默认设置吗？</p>
        <p class="reset-tip">当前的所有自定义设置将会丢失</p>
      </div>
      <div slot="footer" class="dialog-footer-mobile">
        <el-button @click="cancelReset" size="medium">取消</el-button>
        <el-button type="primary" @click="confirmReset" size="medium">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { 
  getNotificationSettings, 
  updateNotificationSettings,
  getDefaultSettings 
} from './service';

export default {
  name: 'MessageNotificationSettings',

  data() {
    return {
      settings: {
        methods: {
          sms: false,
          push: false,
          email: false,
          call: false
        },
        timeRange: {
          start: '08:00',
          end: '22:00'
        },
        types: {
          emergency: true,
          warning: true,
          info: true,
          system: true
        },
        doNotDisturb: {
          enabled: false,
          startTime: '22:00',
          endTime: '08:00',
          emergencyOverride: true
        },
        sound: {
          tone: 'default',
          vibrate: true
        },
        frequency: {
          interval: 15
        },
        preferences: {
          autoRead: true,
          preview: true,
          badge: true
        }
      },
      notificationMethods: [
        {
          label: '短信通知',
          value: 'sms',
          icon: 'el-icon-chat-dot-square',
          description: '通过手机短信接收通知'
        },
        {
          label: '应用推送',
          value: 'push',
          icon: 'el-icon-mobile-phone',
          description: '在手机通知栏显示推送消息'
        },
        {
          label: '邮件通知',
          value: 'email',
          icon: 'el-icon-message',
          description: '发送邮件到注册邮箱'
        },
        {
          label: '电话语音',
          value: 'call',
          icon: 'el-icon-phone',
          description: '特别紧急时拨打电话通知'
        }
      ],
      notificationTypes: [
        {
          label: '紧急事件通知',
          value: 'emergency',
          icon: 'el-icon-warning',
          description: '重大突发事件的即时通知'
        },
        {
          label: '预警提醒',
          value: 'warning',
          icon: 'el-icon-bell',
          description: '风险预警和重要提醒'
        },
        {
          label: '一般信息',
          value: 'info',
          icon: 'el-icon-info',
          description: '日常工作信息和更新'
        },
        {
          label: '系统消息',
          value: 'system',
          icon: 'el-icon-setting',
          description: '系统维护、更新等消息'
        }
      ],
      frequencyMarks: {
        5: '5分',
        15: '15分',
        30: '30分',
        60: '1小时'
      },
      originalSettings: null,
      showResetDialog: false
    };
  },

  computed: {
    hasActiveMethod() {
      return Object.values(this.settings.methods).some(v => v);
    }
  },

  methods: {
    // Toast提示
    showToast(message, type = 'success') {
      this.$notify({
        title: '',
        message: message,
        type: type,
        duration: 2000,
        position: 'top-right',
        offset: 60,
        customClass: 'mobile-toast'
      });
    },

    // 切换通知方式
    toggleMethod(method) {
      this.settings.methods[method] = !this.settings.methods[method];
      this.handleMethodChange();
    },

    // 通知方式变化
    handleMethodChange() {
      if (!this.hasActiveMethod) {
        this.showToast('至少需要开启一种通知方式', 'warning');
      }
    },

    // 免打扰模式变化
    handleDndChange(enabled) {
      if (enabled) {
        this.showToast('免打扰模式已开启', 'info');
      }
    },

    // 保存设置
    handleSave() {
      // 验证时间设置
      if (this.settings.timeRange.start >= this.settings.timeRange.end) {
        this.showToast('开始时间必须早于结束时间', 'error');
        return;
      }

      if (this.settings.doNotDisturb.enabled) {
        if (!this.settings.doNotDisturb.startTime || !this.settings.doNotDisturb.endTime) {
          this.showToast('请设置完整的免打扰时间段', 'error');
          return;
        }
      }

      if (!this.hasActiveMethod) {
        this.showToast('请至少选择一种通知方式', 'error');
        return;
      }

      const loading = this.$loading({
        lock: true,
        text: '保存中...',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      updateNotificationSettings(this.settings).then(() => {
        loading.close();
        this.$notify({
          title: '✅ 保存成功',
          message: '通知设置已更新',
          type: 'success',
          duration: 2500,
          position: 'top-right',
          offset: 60,
          customClass: 'mobile-toast-large'
        });
        this.originalSettings = JSON.parse(JSON.stringify(this.settings));
      }).catch(error => {
        loading.close();
        this.showToast(error.message || '保存失败，请稍后重试', 'error');
      });
    },

    // 恢复默认设置
    handleReset() {
      this.showResetDialog = true;
    },

    // 确认恢复默认
    confirmReset() {
      this.showResetDialog = false;
      const defaultSettings = getDefaultSettings();
      this.settings = JSON.parse(JSON.stringify(defaultSettings));
      this.showToast('已恢复默认设置');
    },

    // 取消恢复默认
    cancelReset() {
      this.showResetDialog = false;
    },

    // 加载通知设置
    loadSettings() {
      const loading = this.$loading({
        lock: true,
        text: '加载中...',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      getNotificationSettings().then(data => {
        this.settings = data;
        this.originalSettings = JSON.parse(JSON.stringify(data));
        loading.close();
      }).catch(error => {
        loading.close();
        this.showToast(error.message || '加载失败', 'error');
      });
    }
  },

  mounted() {
    this.loadSettings();
  }
};
</script>

<style scoped>
.message-notification-settings {
  background: #f5f5f5;
  min-height: 100%;
  padding-bottom: 100px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* 设置区块 */
.setting-section {
  background: white;
  margin-bottom: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.section-title {
  font-size: 15px;
  font-weight: 600;
  color: #333;
  padding: 15px;
  border-left: 3px solid #409EFF;
  padding-left: 12px;
  background: #fafafa;
}

.section-content {
  padding: 10px 15px 15px;
}

/* 通知方式 */
.method-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: background 0.3s;
}

.method-item:last-child {
  border-bottom: none;
}

.method-item:active {
  background: #f8f8f8;
}

.method-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.method-icon {
  font-size: 24px;
  color: #409EFF;
  margin-right: 12px;
  flex-shrink: 0;
}

.method-text {
  flex: 1;
}

.method-name {
  font-size: 15px;
  color: #333;
  margin-bottom: 4px;
  font-weight: 500;
}

.method-desc {
  font-size: 12px;
  color: #999;
}

/* 时间段设置 */
.time-range-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
}

.time-label {
  font-size: 14px;
  color: #666;
  margin-right: 10px;
}

.time-tip {
  display: flex;
  align-items: center;
  padding: 10px;
  background: #f0f9ff;
  border-radius: 4px;
  margin-top: 5px;
}

.time-tip i {
  color: #409EFF;
  margin-right: 8px;
  font-size: 16px;
  flex-shrink: 0;
}

.time-tip span {
  font-size: 12px;
  color: #666;
  line-height: 1.5;
}

/* 通知类型 */
.type-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
}

.type-item:last-child {
  border-bottom: none;
}

.type-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.type-icon {
  font-size: 22px;
  color: #409EFF;
  margin-right: 12px;
  flex-shrink: 0;
}

.type-text {
  flex: 1;
}

.type-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
  font-weight: 500;
}

.type-desc {
  font-size: 12px;
  color: #999;
}

/* 免打扰模式 */
.dnd-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
}

.dnd-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.dnd-icon {
  font-size: 24px;
  color: #409EFF;
  margin-right: 12px;
  flex-shrink: 0;
}

.dnd-text {
  flex: 1;
}

.dnd-name {
  font-size: 15px;
  color: #333;
  margin-bottom: 4px;
  font-weight: 500;
}

.dnd-desc {
  font-size: 12px;
  color: #999;
}

.dnd-time-range {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid #f5f5f5;
}

.dnd-time-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
}

.dnd-time-item:last-child {
  margin-bottom: 0;
}

.emergency-override {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid #f5f5f5;
}

.emergency-override >>> .el-checkbox__label {
  font-size: 13px;
  color: #666;
  line-height: 1.5;
}

/* 音效设置 */
.sound-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
}

.sound-item:last-child {
  border-bottom: none;
}

.sound-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.sound-icon {
  font-size: 22px;
  color: #409EFF;
  margin-right: 12px;
  flex-shrink: 0;
}

.sound-text {
  flex: 1;
}

.sound-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
  font-weight: 500;
}

.sound-desc {
  font-size: 12px;
  color: #999;
}

/* 频率限制 */
.frequency-item {
  padding: 10px 0;
}

.frequency-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

.frequency-item >>> .el-slider {
  padding: 0 15px;
}

.frequency-tip {
  font-size: 13px;
  color: #666;
  text-align: center;
  margin-top: 15px;
  padding: 10px;
  background: #f8f8f8;
  border-radius: 4px;
}

.frequency-tip .highlight {
  color: #409EFF;
  font-weight: 600;
  font-size: 14px;
}

/* 消息推送偏好 */
.preference-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 0;
  border-bottom: 1px solid #f5f5f5;
}

.preference-item:last-child {
  border-bottom: none;
}

.preference-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.preference-icon {
  font-size: 22px;
  color: #409EFF;
  margin-right: 12px;
  flex-shrink: 0;
}

.preference-text {
  flex: 1;
}

.preference-name {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
  font-weight: 500;
}

.preference-desc {
  font-size: 12px;
  color: #999;
}

/* 操作按钮 */
.action-buttons {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 15px;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  gap: 10px;
  z-index: 100;
  border-top: 1px solid #f0f0f0;
}

.action-buttons .el-button {
  flex: 1;
  border-radius: 6px;
  font-size: 15px;
  height: 46px;
  font-weight: 500;
}

/* 恢复默认确认对话框样式 */
.reset-confirm-content {
  text-align: center;
  padding: 20px 0;
}

.reset-icon {
  font-size: 50px;
  color: #E6A23C;
  margin-bottom: 15px;
}

.reset-message {
  font-size: 16px;
  color: #333;
  margin: 0 0 10px 0;
  line-height: 1.6;
}

.reset-tip {
  font-size: 14px;
  color: #999;
  margin: 0;
}

.dialog-footer-mobile {
  display: flex;
  gap: 10px;
  padding: 0;
}

.dialog-footer-mobile .el-button {
  flex: 1;
  margin: 0;
  border-radius: 4px;
}

/* 对话框移动端优化 */
.message-notification-settings >>> .el-dialog {
  border-radius: 12px;
}

.message-notification-settings >>> .el-dialog__header {
  padding: 15px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.message-notification-settings >>> .el-dialog__title {
  font-size: 16px;
  font-weight: 600;
}

.message-notification-settings >>> .el-dialog__body {
  padding: 20px;
}

.message-notification-settings >>> .el-dialog__footer {
  padding: 15px 20px;
  border-top: 1px solid #f0f0f0;
}
</style>

